//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.service;

import com.github.pagehelper.PageHelper;
import com.xw.dao.SysTypeMapper;
import com.xw.dao.VideosMapper;
import com.xw.model.SysType;
import com.xw.model.Videos;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import com.xw.vo.TypeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class TypeServiceImpl implements TypeService {
    @Resource
    private SysTypeMapper typeMapper;
    @Resource
    private VideosMapper videosMapper;

    @Override
    public Map findHotType(Integer page, Integer limit) {
        if (page != null && limit != null){
            PageHelper.startPage(page,limit,false);
        }
        PageHelper.orderBy("is_hot asc");
        List<TypeVo> list = typeMapper.findHotType();

        return JsonData.success(list);
    }

    @Override
    public Map findType() {
        List<TypeVo> list = typeMapper.findTypeOne();

        for (TypeVo type : list){
            List<TypeVo> voList = typeMapper.findTypeByStFid(type.getStId());
            type.setList(voList);
        }

        return JsonData.success(list);
    }

    @Override
    public Map updateTwo(SysType sysType) {
        int flag = typeMapper.updateByPrimaryKeySelective(sysType);
        return JsonData.returnMap(flag);
    }

    @Override
    public Map updateOne(SysType sysType) {
        int flag = typeMapper.updateByPrimaryKeySelective(sysType);
        return JsonData.returnMap(flag);
    }

    @Override
    public SysType findByKeyId(String stId) {
        return typeMapper.selectByPrimaryKey(stId);
    }

    @Override
    public Map del(String stId, Integer stLevel) {
        //判断stLevel 等级
        if (stLevel==1){
            //删除顶级，要判断是否有下级
            SysType type = new SysType();
            type.setStFid(stId);
            type.setStLevel(2);

            int i = typeMapper.selectCount(type);
            if (i>0){
                return JsonData.fail("请先删除下级分类");
            }
            int flag = typeMapper.deleteByPrimaryKey(stId);
            return JsonData.returnMap(flag);
        }else if (stLevel==2){
            //判断该分类有没有被使用
            Videos videos = new Videos();
            videos.setStIdTwo(stId);
            videos.setIsDel(0);
            int i = videosMapper.selectCount(videos);
            if (i > 0){
                return JsonData.fail("该分类已被使用，不能删除");
            }
            int flag = typeMapper.deleteByPrimaryKey(stId);
            return JsonData.returnMap(flag);
        }else if(stLevel==3){
            //判断该分类有没有被使用
            Videos videos = new Videos();
            videos.setStIdThree(stId);
            videos.setIsDel(0);
            int i = videosMapper.selectCount(videos);
            if (i > 0){
                return JsonData.fail("该分类已被使用，不能删除");
            }
            int flag = typeMapper.deleteByPrimaryKey(stId);
            return JsonData.returnMap(flag);
        }
        return JsonData.fail("异常");
    }

    @Override
    public Map addThree(String stName, Integer stSort, String stIdTwo) {
        //查询有没有同名的类型
        SysType type = new SysType();
        type.setStName(stName);
        type.setStFid(stIdTwo);
        type.setStLevel(3);
        int i = typeMapper.selectCount(type);
        if (i > 0){
            return JsonData.fail("该分类下已有此分类");
        }
        type.setStId(AppUtil.uuid());
        type.setCreateTime(new Date());
        type.setStSort(stSort);

        int flag = typeMapper.insertSelective(type);

        return JsonData.returnMap(flag);
    }

    @Override
    public Map findTypeByStFid(String stFId) {
        List<TypeVo> typeList =  typeMapper.findTypeByStFid(stFId);
        return JsonData.success(typeList);
    }

    @Override
    public Map addTwo(String stName, Integer stSort, String stIdOne) {
        //查询有没有同名的类型
        SysType type = new SysType();
        type.setStName(stName);
        type.setStFid(stIdOne);
        type.setStLevel(2);
        int i = typeMapper.selectCount(type);
        if (i > 0){
            return JsonData.fail("该分类下已有此分类");
        }

        type.setStId(AppUtil.uuid());
        type.setCreateTime(new Date());
        type.setStSort(stSort);

        int flag = typeMapper.insertSelective(type);

        return JsonData.returnMap(flag);
    }

    @Override
    public List<TypeVo> findTypeOne() {
        return typeMapper.findTypeOne();
    }

    @Override
    public Map addOne(String stName, Integer stSort) {
        //查询有没有同名的类型
        SysType type = new SysType();
        type.setStName(stName);
        type.setStLevel(1);
        int i = typeMapper.selectCount(type);
        if (i > 0){
            return JsonData.fail("该分类下已有此分类");
        }
        type.setStId(AppUtil.uuid());
        type.setStFid("0");
        type.setCreateTime(new Date());
        type.setStSort(stSort);

        int flag = typeMapper.insertSelective(type);

        return JsonData.returnMap(flag);
    }

    @Override
    public List<TypeVo> findByWhere(TypeVo typeVo) {
        return typeMapper.findByWhere(typeVo);
    }

    @Override
    public List<TypeVo> findTypeByStFid1(String stId) {
      return typeMapper.findTypeByStFid(stId);
    }
}
